package com.yh.hljapp.gddb.gbmanager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.yh.hljapp.AppContext;
import com.yh.hljapp.gddb.gdbean.AddBack_HeadBean;
import com.yh.hljapp.gddb.gdbean.AddBack_HeadBeanDao;
import com.yh.hljapp.gddb.gdbean.AddSaleInt_HeadBean;
import com.yh.hljapp.gddb.gdbean.AddSaleInt_HeadBeanDao;
import com.yh.hljapp.gddb.gdbean.AddSaleInt_bodyBean;
import com.yh.hljapp.gddb.gdbean.AddSaleOut_HeadBean;
import com.yh.hljapp.gddb.gdbean.AddSaleOut_HeadBeanDao;
import com.yh.hljapp.gddb.gdbean.Add_Sync_Bean;
import com.yh.hljapp.gddb.gdbean.Add_Sync_BeanDao;
import com.yh.hljapp.gddb.gdbean.Add_bodyBack_Bean;
import com.yh.hljapp.gddb.gdbean.Add_bodyBack_BeanDao;
import com.yh.hljapp.gddb.gdbean.Add_bodyOut_Bean;
import com.yh.hljapp.gddb.gdbean.Add_bodyOut_BeanDao;
import com.yh.hljapp.gddb.gdbean.Add_body_Bean;
import com.yh.hljapp.gddb.gdbean.Add_body_BeanDao;
import com.yh.hljapp.gddb.gdbean.BackMatBean;
import com.yh.hljapp.gddb.gdbean.BackMatBeanDao;
import com.yh.hljapp.gddb.gdbean.DaoMaster;
import com.yh.hljapp.gddb.gdbean.DaoSession;
import com.yh.hljapp.gddb.gdbean.MatBean;
import com.yh.hljapp.gddb.gdbean.MatBeanDao;
import com.yh.hljapp.gddb.gdbean.MatHead;
import com.yh.hljapp.gddb.gdbean.MatHeadDao;
import com.yh.hljapp.modules.bean.AddSyncBean;

import org.greenrobot.greendao.database.Database;

import java.util.ArrayList;
import java.util.List;

import static android.R.attr.name;
import static android.R.attr.type;
import static android.R.id.list;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：zhanghang
 * <p>
 * 工程：hlj
 * <p>
 * 文件名：AddSale_Manager
 * <p>
 * 创建时间：2017.08.01.9:42
 */

public class AddSale_Manager {
    private final static String dbName = "fb_db";
    private static AddSale_Manager mInstance;
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;
    private final DaoSession mDaoSession;

    public AddSale_Manager(Context context) {
        this.context = context;
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        mDaoSession = daoMaster.newSession();
    }

    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static AddSale_Manager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (AddSale_Manager.class) {
                if (mInstance == null) {
                    mInstance = new AddSale_Manager(context);
                }
            }
        }
        return mInstance;
    }

    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }

    //插入集合
    public boolean insertMatLists(List<Add_Sync_Bean> mbs) {
        Add_Sync_BeanDao add_sync_beanDao = mDaoSession.getAdd_Sync_BeanDao();
        try {
//            add_sync_beanDao.deleteAll();
            add_sync_beanDao.insertOrReplaceInTx(mbs);
        } catch (Exception e) {
            return false;
        }
        return true;

    }

    //判断扫描的出库单是否存在
    public boolean isMatExist(String barcode) {
        Add_Sync_BeanDao matBeanDao = mDaoSession.getAdd_Sync_BeanDao();

        List<Add_Sync_Bean> list = matBeanDao.queryBuilder().where(Add_Sync_BeanDao.Properties.Id.eq(barcode)).list();
        if (list != null && list.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 通过物料唯一的id拿到物料信息
     */
    public Add_Sync_Bean getMatById(String id) {
        Add_Sync_Bean unique = mDaoSession.getAdd_Sync_BeanDao().queryBuilder().where(Add_Sync_BeanDao.Properties.Id.eq(id)).unique();
        return unique;
    }

    //插入集合
    public void insertBackMats(ArrayList<Add_body_Bean> list) {
        mDaoSession.getAdd_body_BeanDao().insertOrReplaceInTx(list);
    }

    //插入集合
    public void insertBackMatsout(ArrayList<Add_bodyOut_Bean> list) {
        mDaoSession.getAdd_bodyOut_BeanDao().insertOrReplaceInTx(list);
    }

    //插入集合
    public void insertBackMatsback(ArrayList<Add_bodyBack_Bean> list) {
        mDaoSession.getAdd_bodyBack_BeanDao().insertOrReplaceInTx(list);
    }

    public void inOrRe(AddSaleInt_HeadBean head) {
        AddSaleInt_HeadBeanDao addSaleInt_headBeanDao = mDaoSession.getAddSaleInt_HeadBeanDao();
        addSaleInt_headBeanDao.insertOrReplace(head);
    }

    public void inOrSale_Out(AddSaleOut_HeadBean head) {
        mDaoSession.getAddSaleOut_HeadBeanDao().insertOrReplace(head);
    }

    public void inOrSale_back(AddBack_HeadBean head) {
        mDaoSession.getAddBack_HeadBeanDao().insertOrReplace(head);
    }

    public List<Add_Sync_Bean> getAddSyncBean() {
        Add_Sync_BeanDao add_sync_beanDao = mDaoSession.getAdd_Sync_BeanDao();
        List<Add_Sync_Bean> add_sync_been = add_sync_beanDao.loadAll();
        return add_sync_been;
    }

    //获得最后一个插入的出库
    public AddSaleOut_HeadBean getLastHead_saleout() {
        AddSaleOut_HeadBeanDao addSaleOut_headBeanDao = mDaoSession.getAddSaleOut_HeadBeanDao();
        List<AddSaleOut_HeadBean> addSaleInt_headBeen = addSaleOut_headBeanDao.loadAll();
        int size = addSaleInt_headBeen.size();
        return addSaleInt_headBeen.get(size - 1);
    }

    //获得最后一个插入的退货
    public AddBack_HeadBean getLastHead_saleback() {
        AddBack_HeadBeanDao addBack_headBeanDao = mDaoSession.getAddBack_HeadBeanDao();
        List<AddBack_HeadBean> addSaleInt_headBeen = addBack_headBeanDao.loadAll();
        int size = addSaleInt_headBeen.size();
        return addSaleInt_headBeen.get(size - 1);
    }

    //获得最后一个插入的入库
    public AddSaleInt_HeadBean getLastHead() {
        AddSaleInt_HeadBeanDao addSaleInt_headBeanDao = mDaoSession.getAddSaleInt_HeadBeanDao();
        List<AddSaleInt_HeadBean> addSaleInt_headBeen = addSaleInt_headBeanDao.loadAll();
        int size = addSaleInt_headBeen.size();
        return addSaleInt_headBeen.get(size - 1);
    }

    //通过id拿到对应的唯一的实体
    public AddSaleInt_HeadBean getHeadById(String id) {
        AddSaleInt_HeadBeanDao addSaleInt_headBeanDao = mDaoSession.getAddSaleInt_HeadBeanDao();
        AddSaleInt_HeadBean unique = addSaleInt_headBeanDao.queryBuilder().where(AddSaleInt_HeadBeanDao.Properties.Id.eq(id)).unique();
        return unique;
    }

    //通过id拿到对应的唯一的实体
    public AddSaleOut_HeadBean getHeadByIdout(String id) {
        AddSaleOut_HeadBeanDao addSaleOut_headBeanDao = mDaoSession.getAddSaleOut_HeadBeanDao();
        AddSaleOut_HeadBean unique = addSaleOut_headBeanDao.queryBuilder().where(AddSaleOut_HeadBeanDao.Properties.Id.eq(id)).unique();
        return unique;
    }

    public AddBack_HeadBean getHeadByIdback(String id) {
        AddBack_HeadBeanDao addBack_headBeanDao = mDaoSession.getAddBack_HeadBeanDao();
        AddBack_HeadBean unique = addBack_headBeanDao.queryBuilder().where(AddBack_HeadBeanDao.Properties.Id.eq(id)).unique();
        return unique;
    }

    public List<Add_body_Bean> getBackMatsByHeadId(long headId) {
        Add_body_BeanDao add_body_beanDao = mDaoSession.getAdd_body_BeanDao();
        List<Add_body_Bean> list = add_body_beanDao.queryBuilder().where(Add_body_BeanDao.Properties.HeadId.eq(headId)).list();
        return list;
    }

    public List<Add_bodyOut_Bean> getBackMatsByHeadIdout(long headId) {
        Add_bodyOut_BeanDao add_bodyOut_beanDao = mDaoSession.getAdd_bodyOut_BeanDao();
        List<Add_bodyOut_Bean> list = add_bodyOut_beanDao.queryBuilder().where(Add_bodyOut_BeanDao.Properties.HeadId.eq(headId)).list();
        return list;
    }

    public List<Add_bodyBack_Bean> getBackMatsByHeadIdback(long headId) {
        Add_bodyBack_BeanDao add_bodyBack_beanDao = mDaoSession.getAdd_bodyBack_BeanDao();
        List<Add_bodyBack_Bean> list = add_bodyBack_beanDao.queryBuilder().where(Add_bodyBack_BeanDao.Properties.HeadId.eq(headId)).list();
        return list;
    }

    public List<AddSaleInt_HeadBean> getHeadsByNa(String name) {

        AddSaleInt_HeadBeanDao addSaleInt_headBeanDao = mDaoSession.getAddSaleInt_HeadBeanDao();

        List<AddSaleInt_HeadBean> list = addSaleInt_headBeanDao.queryBuilder().where(AddSaleInt_HeadBeanDao.Properties.Username.eq(name)).list();
        return list;

    }

    public List<AddSaleOut_HeadBean> getHeadsByNaout(String name) {

        AddSaleOut_HeadBeanDao addSaleOut_headBeanDao = mDaoSession.getAddSaleOut_HeadBeanDao();

        List<AddSaleOut_HeadBean> list = addSaleOut_headBeanDao.queryBuilder().where(AddSaleOut_HeadBeanDao.Properties.Username.eq(name)).list();
        return list;

    }

    public List<AddBack_HeadBean> getHeadsByNaback(String name) {

        AddBack_HeadBeanDao addBack_headBeanDao = mDaoSession.getAddBack_HeadBeanDao();

        List<AddBack_HeadBean> list = addBack_headBeanDao.queryBuilder().where(AddBack_HeadBeanDao.Properties.Username.eq(name)).list();
        return list;

    }

    public void delAllById(long id) {
        String name = AppContext.getInstance().getCurrentUser().getName();
        AddSaleInt_HeadBeanDao addSaleInt_headBeanDao = mDaoSession.getAddSaleInt_HeadBeanDao();
        List<AddSaleInt_HeadBean> list = addSaleInt_headBeanDao.queryBuilder().where(AddSaleInt_HeadBeanDao.Properties.Username.eq(name), AddSaleInt_HeadBeanDao.Properties.Id.eq(id)).list();
        addSaleInt_headBeanDao.deleteInTx(list);
    }

    public void delAllByIdBack(long id) {
        String name = AppContext.getInstance().getCurrentUser().getName();
        AddBack_HeadBeanDao addBack_headBeanDao = mDaoSession.getAddBack_HeadBeanDao();
        List<AddBack_HeadBean> list = addBack_headBeanDao.queryBuilder().where(AddBack_HeadBeanDao.Properties.Username.eq(name), AddBack_HeadBeanDao.Properties.Id.eq(id)).list();
        addBack_headBeanDao.deleteInTx(list);
    }

    public void delAllByIdOut(long id) {
        String name = AppContext.getInstance().getCurrentUser().getName();
        AddSaleOut_HeadBeanDao addSaleOut_headBeanDao = mDaoSession.getAddSaleOut_HeadBeanDao();
        List<AddSaleOut_HeadBean> list = addSaleOut_headBeanDao.queryBuilder().where(AddSaleOut_HeadBeanDao.Properties.Username.eq(name), AddSaleOut_HeadBeanDao.Properties.Id.eq(id)).list();
        addSaleOut_headBeanDao.deleteInTx(list);
    }

    public void delAllByHeadId(long headId) {
        String name = AppContext.getInstance().getCurrentUser().getName();
        Add_body_BeanDao add_body_beanDao = mDaoSession.getAdd_body_BeanDao();
        List<Add_body_Bean> list = add_body_beanDao.queryBuilder().where(Add_body_BeanDao.Properties.UserName.eq(name), Add_body_BeanDao.Properties.HeadId.eq(headId)).list();
        add_body_beanDao.deleteInTx(list);
    }

    public void delAllByHeadIdBack(long headId) {
        String name = AppContext.getInstance().getCurrentUser().getName();
        Add_bodyBack_BeanDao add_bodyBack_beanDao = mDaoSession.getAdd_bodyBack_BeanDao();
        List<Add_bodyBack_Bean> list = add_bodyBack_beanDao.queryBuilder().where(Add_bodyBack_BeanDao.Properties.UserName.eq(name), Add_bodyBack_BeanDao.Properties.HeadId.eq(headId)).list();
        add_bodyBack_beanDao.deleteInTx(list);
    }

    public void delAllByHeadIdOut(long headId) {
        String name = AppContext.getInstance().getCurrentUser().getName();
        Add_bodyOut_BeanDao add_bodyOut_beanDao = mDaoSession.getAdd_bodyOut_BeanDao();
        List<Add_bodyOut_Bean> list = add_bodyOut_beanDao.queryBuilder().where(Add_bodyOut_BeanDao.Properties.UserName.eq(name), Add_bodyOut_BeanDao.Properties.HeadId.eq(headId)).list();
        add_bodyOut_beanDao.deleteInTx(list);
    }
}
